package day13;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
* Map存储自定义的引用类作为key值
*
* */
public class Demo8 {
    public static void main(String[] args) {

        //创建Map集合对象
        Map<Student,String> map = new HashMap<>();

        Student s1 = new Student("张三",18);
        Student s2 = new Student("李四",30);
        Student s3 = new Student("张三",18);

        //放入到集合中
        System.out.println(map.put(s1,"北京市"));
        System.out.println(map.put(s2,"天津市"));
        System.out.println(map.put(s3,"上海市"));

        /*
        * 遍历map集合
        * */

        //将map中的一个个键值对封装为一个个entry对象，
        // 然后将entry对象全部取出放入到一个Set集合中
        Set<Map.Entry<Student,String>> set = map.entrySet();

        for (Map.Entry<Student, String> entry: set){
            System.out.println(entry.getKey()+":"+entry.getValue());
        }

    }
}
